<template>
    <div class="hot-border">
        <div v-for="(event, index) in displayedEvents" :key="index" class="event-item">
            <div class="event-details">
                <h4 class="event-title">{{ event.title }}</h4>
                <p class="event-detail">{{ event.detail }}</p>
                <p class="event-time">{{ formatTime(event.time) }}</p>
            </div>
            <div v-if="index < displayedEvents.length - 1" class="event-separator"></div>
        </div>
        <div v-if="hotEvents.length > 4" class="more-events">
            <button style="border: none; background-color: white; color:black;" @click="viewMore">查看更多...</button>
        </div>
    </div>
</template>

<script>
import axios from 'axios'
export default {
    data() {
        return {
            hotEvents: [
                { title: '热点事件1', detail: 'dgsakfhajskdhsadhjkas dgsakfhajskdhsadhjkas dgsakfhajskdhsadhjkas', time: '2023-10-01' },
                { title: '热点事件2', detail: 'dgsakfhajskdhsadhjkas dgsakfhajskdhsadhjkas dgsakfhajskdhsadhjkas', time: '2023-10-02' },
                { title: '热点事件3', detail: 'dgsakfhajskdhsadhjkas dgsakfhajskdhsadhjkas dgsakfhajskdhsadhjkas', time: '2023-10-03' }
            ]
        };
    },
    computed: {
        displayedEvents() {
            return this.hotEvents.slice(0, 4);
        }
    },
    methods: {
        async getEvent() {
            this.loginError = '';
            try {
                const response = await axios.get('http://47.108.80.35:6060/hot_event/get_event');
                console.log("请求事件", response);
                this.hotEvents = response.data.data.map(event => ({
                    title: event.title,
                    detail: event.content,
                    time: event.event_time
                }));
                console.log("事件", this.hotEvents);
            } catch (error) {
                console.log("请求失败", error);
            }
        },
        viewMore() {
            this.$router.push({ name: 'event' });
        },
        formatTime(time) {
            const date = new Date(time);
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            const hours = String(date.getHours()).padStart(2, '0');
            const minutes = String(date.getMinutes()).padStart(2, '0');
            const seconds = String(date.getSeconds()).padStart(2, '0');
            return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        }
    },
    created() {
        this.getEvent();
    }
};
</script>

<style scoped>
.hot-border {
    border: 1px solid #ccc;
    padding: 16px;
    max-width: 400px;
    margin: 0 auto;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 添加阴影 */
    height: 400px;
    overflow: hidden; /* 隐藏溢出内容 */
}

.event-item {
    position: relative;
    margin-bottom: 16px;
    font-size:small;
}

.event-details {
    display: flex;
    flex-direction: column; /* 修改为列布局 */
}

.event-title {
    margin: 0 0 8px 8px;
    font-size: 18px;
    text-align: left;
}

.event-detail {
    margin: 0 0 8px 8px;
    color: #666;
    white-space: nowrap; /* 防止换行 */
    overflow: hidden; /* 隐藏溢出内容 */
    text-overflow: ellipsis; /* 显示省略号 */
    max-width: 100%; /* 确保宽度不超过容器 */
}

.event-time {
    margin: 0 0 0 8px;
    color: #999;
    font-size: 14px;
    text-align: right;
}

.event-separator {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 1px;
    background-color: #ccc;
}

.more-events {
    margin-top: 16px;
    text-align: right;
    font-size: 16px;
}

.more-events button {
    padding: 8px 16px;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

.more-events button:hover {
    background-color: #0056b3;
}
</style>